from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from src.config.config import app_config

Base = declarative_base()


class MySQLDatabase:

    def __init__(self):
        # 数据库连接字符串
        database_uri = app_config.database_url
        # 创建数据库引擎
        self.engine = create_engine(database_uri, echo=True)
        Base.metadata.create_all(self.engine)
        # 创建一个Session类
        _Session = sessionmaker(bind=self.engine)
        self.session = _Session()

    def close(self):
        self.session.close()


sqldb = MySQLDatabase()
